package com.quectel.core.module.partyeducationexam.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.partyeducationexam.dao.PartyEducationQuestionBankPartyDao;
import com.quectel.core.module.partyeducationexam.dto.PartyEducationQuestionBankPartyDto;
import com.quectel.core.module.partyeducationexam.entity.PartyEducationQuestionBankPartyEntity;
import com.quectel.core.module.partyeducationexam.service.PartyEducationQuestionBankPartyService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/06/19 10:38
 */
@DubboService
public class PartyEducationQuestionBankPartyServiceImpl extends ServiceImpl<PartyEducationQuestionBankPartyDao, PartyEducationQuestionBankPartyEntity> implements PartyEducationQuestionBankPartyService{

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyEducationQuestionBankPartyServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARTY_EDUCATION_QUESTION_BANK_PARTY_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARTY_EDUCATION_QUESTION_BANK_PARTY_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PartyEducationQuestionBankPartyDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARTY_EDUCATION_QUESTION_BANK_PARTY_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PartyEducationQuestionBankPartyDao partyEducationQuestionBankPartyDao;


    @Override
    public List<PartyEducationQuestionBankPartyDto> selectByBankId(Long bankId) {
        List<PartyEducationQuestionBankPartyEntity> partyEducationQuestionBankPartyEntities = partyEducationQuestionBankPartyDao.selectList(new LambdaQueryWrapper<PartyEducationQuestionBankPartyEntity>()
                .eq(PartyEducationQuestionBankPartyEntity::getBankId, bankId));
        return CopyUtils.copyList(partyEducationQuestionBankPartyEntities, PartyEducationQuestionBankPartyDto.class);
    }

    @Override
    public void deleteByBankId(Long bankId) {
        partyEducationQuestionBankPartyDao.delete(new LambdaQueryWrapper<PartyEducationQuestionBankPartyEntity>()
                .eq(PartyEducationQuestionBankPartyEntity::getBankId, bankId));
    }

    @Override
    public void saveBatch(List<PartyEducationQuestionBankPartyDto> dtos) {
        List<PartyEducationQuestionBankPartyEntity> partyEducationQuestionBankPartyEntities = CopyUtils.copyList(dtos, PartyEducationQuestionBankPartyEntity.class);
        for (PartyEducationQuestionBankPartyEntity partyEducationQuestionBankPartyEntity : partyEducationQuestionBankPartyEntities) {
            partyEducationQuestionBankPartyEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(partyEducationQuestionBankPartyEntities);
    }

    @Override
    public PartyEducationQuestionBankPartyDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARTY_EDUCATION_QUESTION_BANK_PARTY_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PartyEducationQuestionBankPartyDto selectById(Long id){
            PartyEducationQuestionBankPartyEntity entity = partyEducationQuestionBankPartyDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,PartyEducationQuestionBankPartyDto.class));
    }

    @Override
    public List<PartyEducationQuestionBankPartyDto> queryList(Map<String,Object> params){
        List<PartyEducationQuestionBankPartyEntity> list = partyEducationQuestionBankPartyDao.queryList(params);
        List<PartyEducationQuestionBankPartyDto> result =  CopyUtils.copyList(list,PartyEducationQuestionBankPartyDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<PartyEducationQuestionBankPartyDto> queryListWithoutPadding(Map<String,Object> params){
        List<PartyEducationQuestionBankPartyEntity> list = partyEducationQuestionBankPartyDao.queryList(params);
        return CopyUtils.copyList(list,PartyEducationQuestionBankPartyDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PartyEducationQuestionBankPartyDto paddingField(PartyEducationQuestionBankPartyDto dto){
        if (dto != null){

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return partyEducationQuestionBankPartyDao.queryTotal(params);
    }

    @Override
    public Long save(PartyEducationQuestionBankPartyDto dto){
        PartyEducationQuestionBankPartyEntity entity = CopyUtils.copyObj(dto,PartyEducationQuestionBankPartyEntity.class);
        entity.setId(Snowflake.nextId());
        partyEducationQuestionBankPartyDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PartyEducationQuestionBankPartyDto dto){
        PartyEducationQuestionBankPartyEntity entity = CopyUtils.copyObj(dto,PartyEducationQuestionBankPartyEntity.class);

        partyEducationQuestionBankPartyDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PartyEducationQuestionBankPartyDto dto) {
       PartyEducationQuestionBankPartyEntity entity = CopyUtils.copyObj(dto,PartyEducationQuestionBankPartyEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, partyEducationQuestionBankPartyDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        partyEducationQuestionBankPartyDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
